package com.cryptoneedle.center.doris.service.doris;

import cn.hutool.v7.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.center.common.entity.doris.DorisColumn;
import com.cryptoneedle.center.common.key.ColumnKey;
import com.cryptoneedle.center.common.repository.doris.DorisColumnRepository;
import com.cryptoneedle.center.doris.repository.metadata.DorisMetadataRepository;
import com.cryptoneedle.center.doris.service.Information.DorisInformationColumnService;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Column </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@DS("pg")
@Slf4j
public class DorisColumnService extends BaseService<DorisColumn, ColumnKey> {

    @Resource
    private DorisColumnService dorisColumnService;

    @Resource
    private DorisColumnRepository dorisColumnRepository;

    @Resource
    private DorisInformationColumnService dorisInformationColumnService;

    @Resource
    private DorisMetadataRepository dorisMetadataRepository;


    @Override
    public BaseRepository<DorisColumn, ColumnKey> getRepository() {
        return this.dorisColumnRepository;
    }


    @Transactional
    public List<DorisColumn> list() {
        return dorisColumnRepository.list();
    }

    @Transactional
    public List<DorisColumn> listByDatabase(String catalogName, String databaseName) {
        return dorisColumnRepository.listByDatabase(catalogName, databaseName);
    }

    @Transactional
    public List<DorisColumn> listByTable(String catalogName, String databaseName, String tableName) {
        return dorisColumnRepository.listByTable(catalogName, databaseName, tableName);
    }

    @Transactional
    public void addList(List<DorisColumn> list) {
        dorisColumnRepository.saveAll(list);
    }

    @Transactional
    public void saveList(List<DorisColumn> list) {
        dorisColumnRepository.saveAll(list);
    }

    @Transactional
    public void deleteList(List<DorisColumn> list) {
        dorisColumnRepository.deleteAll(list);
    }

    public List<DorisColumn> syncDorisColumn(String catalogName, String databaseName) {
        // 查询所有
        List<DorisColumn> originList = dorisColumnService.listByDatabase(catalogName, databaseName);

        // 待同步数据
        List<DorisColumn> dealList = dorisInformationColumnService.listByDatabase(catalogName, databaseName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisColumn.class))
                .toList();

        // 比较插入
        syncCompare(originList, dealList);

        return dorisColumnService.listByDatabase(catalogName, databaseName);
    }

    public List<DorisColumn> syncDorisColumn(String catalogName, String databaseName, String tableName) {
        // 查询所有
        List<DorisColumn> originList = dorisColumnService.listByTable(catalogName, databaseName, tableName);

        // 待同步数据
        List<DorisColumn> dealList = dorisInformationColumnService.listByTable(catalogName, databaseName, tableName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisColumn.class))
                .toList();

        syncCompare(originList, dealList);

        return dorisColumnService.listByTable(catalogName, databaseName, tableName);
    }

    public void syncCompare(List<DorisColumn> originList, List<DorisColumn> dealList) {
        Map<String, DorisColumn> originMap = Maps.uniqueIndex(originList, DorisColumn::getKeyString);
        Map<String, DorisColumn> dealMap = Maps.uniqueIndex(dealList, DorisColumn::getKeyString);

        // 新增数据
        List<DorisColumn> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.getKeyString())).toList();

        // 移除数据
        List<DorisColumn> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.getKeyString())).toList();

        // 保存数据
        List<DorisColumn> saveList = dealList
                .stream()
                .filter(deal -> originMap.containsKey(deal.getKeyString()))
                .peek(deal -> {
                    DorisColumn origin = originMap.get(deal.getKeyString());
                }).toList();

        dorisColumnService.addList(addList);
        dorisColumnService.saveList(saveList);
        dorisColumnService.deleteList(removeList);
    }
}